<?php
//fonction CLIENT
	
	function add_folder($name,$chemin){
		$name = mysql_real_escape_string(htmlspecialchars($name));
		$chemin = mysql_real_escape_string(htmlspecialchars($chemin));
		
		$sql_query=mysql_query("Select name,chemin from ".DATABASE_TABLE_FOLDER." ORDER BY `name` ASC") or die(mysql_error());
		while ($query = mysql_fetch_array($sql_query) ){
			if ($query['chemin'] == $chemin){
				$_SESSION['error'] = "Chemin déja existant";
				return false;
			}elseif ($query['name'] == $name){
				$_SESSION['error'] = "name déja existant";
				return false;
			}
			if (! check_name_folder($query['chemin'],$chemin)){
				$_SESSION['error']="Dossier parent ou des dossiers fils sont déja partagé supprimé d'abord les partages qui font conflit";
				return false;
			}
		}
		if (substr($chemin,-1) != "/"){
			$chemin = $chemin."/";
		}
		if (substr($chemin,0,1) != "/"){
			$_SESSION['error']="Le chemin doit être absolu";
		}
		if (! is_dir($chemin)){
			$_SESSION['error']="Dossier invalide ou droit insuffisant";
			return false;
		}
		else{
			mysql_query("INSERT INTO ".DATABASE_TABLE_FOLDER." VALUES(LAST_INSERT_ID() ,'$name', '$chemin')") or die(mysql_error());
			return true;
		}
	}
	
	function check_name_folder($chemin1,$chemin2){
		if(strlen($chemin1)>strlen($chemin2)){
			$count = strlen($chemin2);
		}
		else{
			$count = strlen($chemin1);
		}
		for ($i=0;$i<($count-1);$i++){
			if(substr($chemin1,$i,1) != substr($chemin2,$i,1) ){
				return true;
			}
		}
		return false;
	}
	
	function add_server($name,$address,$pin){
		$name = mysql_real_escape_string(htmlspecialchars($name));
		$address = mysql_real_escape_string(htmlspecialchars($address));
		$pin = mysql_real_escape_string(htmlspecialchars($pin));
		
		$sql_query=mysql_query("Select name,pin,address from ".DATABASE_TABLE_SERVER." ORDER BY `name` ASC") or die(mysql_error());
		while ($query = mysql_fetch_array($sql_query) ){
			if ($query['pin'] == $pin){
				$_SESSION['error'] = "Pin déja existant";
				return false;
			}elseif ($query['name'] == $name){
				$_SESSION['error'] = "name déja existant";
				return false;
			}elseif ($query['address'] == $address){
				$_SESSION['error'] = "address déja existant";
				return false;
			}
		}
		mysql_query("INSERT INTO ".DATABASE_TABLE_SERVER." VALUES(LAST_INSERT_ID() ,'$name', '$address','$pin')") or die(mysql_error());
		return true;
	}
	
	function remove_folder($box){
		$tablength = count($box);
		$i = 0;
		while ($i!=$tablength)
		{
			mysql_query("DELETE FROM ".DATABASE_TABLE_FOLDER." WHERE `id` = $box[$i]") or die(mysql_error());
			$i++;
		}
	}
	
	function remove_server($box){
		$tablength = count($box);
		$i = 0;
		while ($i!=$tablength)
		{
			mysql_query("DELETE FROM ".DATABASE_TABLE_SERVER." WHERE `id` = $box[$i]") or die(mysql_error());
			$i++;
		}
	}
	
	function check_version(){
		$version = "0.0.1";
		return $version;
	}
	
	function check_recurse_folder($rep,&$share_file){
			$dir = opendir($rep);
			$pathrep = $rep;
			while ($file = readdir($dir)) {
				if(is_file($rep.$file)){
					$i = count($share_file);
					//gestion des gros fichiers
						//$size = sprintf("%u", filesize($rep.$file));// filesize($rep.$file);
						$size =filesize($rep.$file);
					$share_file[$i][0] = $file;
					$share_file[$i][1] = $size;
					$share_file[$i][2] = filectime($rep.$file);
					$share_file[$i][3] = $pathrep.$file;
				}
				elseif(is_dir($rep.$file) and $file != "." and $file != ".."){
					check_recurse_folder($rep.$file."/",$share_file);
				}
			}
	}
	
	function array_folder(){
		$i=0;
		$sql_query = mysql_query("Select chemin from ".DATABASE_TABLE_FOLDER." ORDER BY `name` ASC");
		while ($query = mysql_fetch_array($sql_query)){
			$share_file;
			check_recurse_folder($query['chemin'],$share_file);
				
		}		
		return serialize($share_file);
	}
	
	function check_pin_serv($getpin){
		$sql_query = mysql_query("Select name,pin from ".DATABASE_TABLE_SERVER." ORDER BY `name` ASC");
			while ($query = mysql_fetch_array($sql_query)){
				if (md5($query['pin']) == $getpin){
					return $query['name'];	
				}
			}
			return false;
	}
	
	function return_array($getpin){
		$check_pass = check_pin_serv($getpin);
		if ($check_pass != false){
			print "<version>".check_version()."</version><filesearch>".array_folder()."</filesearch>";
			exit();
		}
		else{
			print "Bad password";
			exit();
		}
	}
	
	function login_user($login,$pass){
		$sql_query = mysql_query("Select name,md5 from ".DATABASE_TABLE_USER." ORDER BY `id` ASC");
		while ($query = mysql_fetch_array($sql_query)){
			if ($query['name'] == $login){
				if ($query['md5']== md5($pass)){
					$_SESSION['exist']=1;
					return true;
				}
			}
		}
		return false;
	}
	
	function disconnect_user(){
			$_SESSION['exist']=0;
	}
	
	function file_to_path($file){
		$file_array=unserialize(array_folder());
		for ($i=0;$i<count($file_array);$i++){
			if ($file == $file_array[$i][0]) {
				return $file_array[$i][3];
			}
		}
		return false;
	}
	
	
	function id_generate(){
		$string = "";
		$chaine = "abcdefghijklmnpqrstuvwxy0123456789";
		for ($i=0;$i<30;$i++){
			$nb = rand(0,strlen($chaine)-1);
			$string = $string.substr($chaine,$nb,1);
		}
		return $string;
	}
	
	function md5_to_file($md5){
		$file_array=unserialize(array_folder());
		for ($i=0;$i<count($file_array);$i++){
			//print mysql_escape_string( $file_array[$i][0])."<br />";
			
			if (md5(mysql_escape_string($file_array[$i][0]))==$md5){
				return $file_array[$i][0];
			}
		}
		print "invalide file";exit;
	}
	
	function register_id($md5,$user,$server){
		$id = id_generate();
		$film = md5_to_file($md5);
		mysql_query("INSERT INTO ".DATABASE_TABLE_ID."(id_generate,film,date,user,server,valid) VALUES('$id', '".mysql_escape_string($film)."','".time()."','".mysql_escape_string($user)."','".mysql_escape_string($server)."',1)") or die(mysql_error());
		//log
		mysql_query("INSERT INTO ".DATABASE_TABLE_LOG."(action,server,user,date) VALUES('Gener ID pour ".mysql_escape_string($film)."','".mysql_escape_string($server)."' ,'".mysql_escape_string($user)."','".time()."')") or die(mysql_error());
    	
		return $id;
	}
	
	

?>